package com.dongdongshop.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.mapper.*;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.TbGoodsServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Stream;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class TbGoodsServiceImpl implements TbGoodsServiceI {

    //商品新增
    @Autowired
    private TbGoodsDescMapper tbGoodsDescMapper;

    @Autowired
    private TbGoodsMapper tbGoodsMapper;

    //商品类
    @Autowired
    private TbItemCatMapper tbItemCatMapper;

    @Autowired
    private TbTypeTemplateMapper tbTypeTemplateMapper;

    //规格
    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;

    @Autowired
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;

    @Autowired
    private TbItemMapper tbItemMapper;

    @Autowired
    private TbBrandMapper tbBrandMapper;

    @Autowired
    private TbSellerMapper tbSellerMapper;

    @Override
    @Transactional
    public List<TbItemCat> getTbItemCat(Long pid) {
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(pid);
        return tbItemCatMapper.selectByExample(example);
    }

    @Override
    @Transactional
    public TbTypeTemplate selectTbTypeTemplateById(Long id) {
        //根据三级联动最后的pid当去id查询完数据取出typeId
        TbItemCat tbItemCat = tbItemCatMapper.selectByPrimaryKey(id);
        Long typeId = tbItemCat.getTypeId();
        TbTypeTemplate tbTypeTemplate = tbTypeTemplateMapper.selectByPrimaryKey(typeId);
        return tbTypeTemplate;
    }

    @Override
    @Transactional
    public void savetbGoods(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc, String items, TbItem tbItem) {
        //增加返回ID
        tbGoodsMapper.savetbGoods(tbGoods);
        tbGoodsDesc.setGoodsId( tbGoods.getId());
        //增加第二张表
        tbGoodsDescMapper.savetbGoodss(tbGoodsDesc);

        //商品item
        List<TbItem> list = JSONObject.parseArray(items,TbItem.class);

        Stream<TbItem> stream = list.stream();
        Consumer<TbItem> consumer = item ->{
            item.setTitle(tbGoods.getGoodsName());
            item.setSellPoint(tbGoods.getCaption());
            item.setPrice(item.getPrice());
            item.setNum(item.getNum());
            item.setImage(tbItem.getImage());
            item.setCategoryid(tbGoods.getCategory3Id());
            item.setStatus("0");
            item.setCreateTime(new Date());
            item.setUpdateTime(new Date());
            item.setGoodsId(tbGoods.getId());
            item.setSellerId(tbGoods.getSellerId());
            item.setCategory(tbItemCatMapper.selectByPrimaryKey(tbGoods.getCategory3Id()).getName());
            item.setBrand(tbBrandMapper.selectByPrimaryKey(tbGoods.getBrandId()).getName());
            item.setSpec(item.getSpec());
            item.setSeller(tbSellerMapper.selectByPrimaryKey(tbGoods.getSellerId()).getName());
            tbItemMapper.insert(item);
        };
        stream.forEach(consumer);

    }

    @Override
    @Transactional
    public Map<Integer, List<TbSpecificationOption>> getFildAll(Integer[] specIds) {
        Map<Integer, List<TbSpecificationOption>> map = new HashMap<>();
        Stream<Integer> stream = Arrays.stream(specIds);
        Consumer<Integer> consumer = specId ->{
            TbSpecificationOptionExample example = new TbSpecificationOptionExample();
            TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
            criteria.andSpecIdEqualTo(specId.longValue());
            List<TbSpecificationOption> list = tbSpecificationOptionMapper.selectByExample(example);
            map.put(specId,list);
        };
        stream.forEach(consumer);
        return map;
    }

    @Override
    public List<TbGoodsVo> getFindAlls(String id,String auditStatus,String goodsName) {
        return tbGoodsMapper.getFindAlls(id,auditStatus,goodsName);
    }

    @Override
    @Transactional
    public void UpdateGoods(List<Long> list) {
        Stream<Long> stream = list.stream();
        Consumer<Long> consumer = id ->{
            TbGoods tbGoods = tbGoodsMapper.selectByPrimaryKey(id);
            tbGoods.setIsDelete("2");
            tbGoodsMapper.updateByPrimaryKeySelective(tbGoods);
        };
        stream.forEach(consumer);
    }

    @Override
    @Transactional
    public void sumitGoods(List<Long> list) {
        Stream<Long> stream = list.stream();
        Consumer<Long> consumer = id ->{
            TbGoods tbGoods = tbGoodsMapper.selectByPrimaryKey(id);
            tbGoods.setAuditStatus("0");
            tbGoodsMapper.updateByPrimaryKeySelective(tbGoods);
        };
        stream.forEach(consumer);
    }

    @Override
    public List<TbGoodsVo> getFindById(String goodsName) {
        return tbGoodsMapper.getFindById(goodsName);
    }

    @Override
    @Transactional
    public void UpdateBackgroundGoods(List<Long> list) {
        Stream<Long> stream = list.stream();
        Consumer<Long> consumer = id ->{
            TbGoods tbGoods = tbGoodsMapper.selectByPrimaryKey(id);
            tbGoods.setAuditStatus("2");
            tbGoodsMapper.updateByPrimaryKeySelective(tbGoods);
        };
        stream.forEach(consumer);
    }

    @Override
    public void rejectedGoods(List<Long> list) {
        Stream<Long> stream = list.stream();
        Consumer<Long> consumer = id ->{
            TbGoods tbGoods = tbGoodsMapper.selectByPrimaryKey(id);
            tbGoods.setAuditStatus("3");
            tbGoodsMapper.updateByPrimaryKeySelective(tbGoods);
        };
        stream.forEach(consumer);
    }

    @Override
    public Map<Object, Object> getFildAllId(Long id) {
        Map<Object,Object> map = new HashMap<>();
        VoTbGoodsDesc voTbGoodsDesc = tbGoodsMapper.getFildAllId(id);
        List<TbItem> tbItems = tbItemMapper.getList(id);
        map.put("1",voTbGoodsDesc);
        map.put("2",tbItems);
        return map;
    }

    @Override
    public TbGoods findGoodsById(Long goodsId) {
        return tbGoodsMapper.selectByPrimaryKey(goodsId);
    }
}
